home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / system-config-printer / troubleshoot / CheckPrinterSanity.py < prev    next >
Encoding:
Python Source  |  2010-09-28  |  5.4 KB  |  134 lines

  1. #!/usr/bin/env python
  2.  
  3. ## Printing troubleshooter
  4.  
  5. ## Copyright (C) 2008, 2009, 2010 Red Hat, Inc.
  6. ## Authors:
  7. ##  Tim Waugh <twaugh@redhat.com>
  8.  
  9. ## This program is free software; you can redistribute it and/or modify
  10. ## it under the terms of the GNU General Public License as published by
  11. ## the Free Software Foundation; either version 2 of the License, or
  12. ## (at your option) any later version.
  13.  
  14. ## This program is distributed in the hope that it will be useful,
  15. ## but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. ## GNU General Public License for more details.
  18.  
  19. ## You should have received a copy of the GNU General Public License
  20. ## along with this program; if not, write to the Free Software
  21. ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  
  23. import cups
  24. import gobject
  25. import os
  26. import smburi
  27. import subprocess
  28. from timedops import TimedOperation, TimedSubprocess
  29. import urllib
  30. from base import *
  31. class CheckPrinterSanity(Question):
  32.     def __init__ (self, troubleshooter):
  33.         Question.__init__ (self, troubleshooter, "Check printer sanity")
  34.         troubleshooter.new_page (gtk.Label (), self)
  35.         self.troubleshooter = troubleshooter
  36.  
  37.     def display (self):
  38.         # Collect information useful for the various checks.
  39.  
  40.         self.answers = {}
  41.  
  42.         answers = self.troubleshooter.answers
  43.         if not answers['cups_queue_listed']:
  44.             return False
  45.  
  46.         name = answers['cups_queue']
  47.  
  48.         parent = self.troubleshooter.get_window ()
  49.  
  50.         # Find out if this is a printer or a class.
  51.         try:
  52.             cups.setServer ('')
  53.             c = TimedOperation (cups.Connection, parent=parent).run ()
  54.             printers = TimedOperation (c.getPrinters, parent=parent).run ()
  55.             if printers.has_key (name):
  56.                 self.answers['is_cups_class'] = False
  57.                 queue = printers[name]
  58.                 self.answers['cups_printer_dict'] = queue
  59.             else:
  60.                 self.answers['is_cups_class'] = True
  61.                 classes = TimedOperation (c.getClasses, parent=parent).run ()
  62.                 queue = classes[name]
  63.                 self.answers['cups_class_dict'] = queue
  64.  
  65.             attrs = TimedOperation (c.getPrinterAttributes, (name,),
  66.                                     parent=parent).run ()
  67.             self.answers['local_cups_queue_attributes'] = attrs
  68.         except:
  69.             pass
  70.  
  71.         if self.answers.has_key ('cups_printer_dict'):
  72.             cups_printer_dict = self.answers['cups_printer_dict']
  73.             uri = cups_printer_dict['device-uri']
  74.             (scheme, rest) = urllib.splittype (uri)
  75.             self.answers['cups_device_uri_scheme'] = scheme
  76.             if scheme in ["ipp", "http", "https"]:
  77.                 (hostport, rest) = urllib.splithost (rest)
  78.                 (host, port) = urllib.splitnport (hostport, defport=631)
  79.                 self.answers['remote_server_name'] = host
  80.                 self.answers['remote_server_port'] = port
  81.             elif scheme == "smb":
  82.                 u = smburi.SMBURI (uri)
  83.                 (group, host, share, user, password) = u.separate ()
  84.                 os.environ['HOST'] = host
  85.                 if group:
  86.                     os.environ['GROUP'] = group
  87.                     cmdline = 'LC_ALL=C nmblookup -W "$GROUP" "$HOST"'
  88.                 else:
  89.                     cmdline = 'LC_ALL=C nmblookup "$HOST"'
  90.                 try:
  91.                     p = TimedSubprocess (parent=parent,
  92.                                          timeout=5000,
  93.                                          args=cmdline, shell=True,
  94.                                          close_fds=True,
  95.                                          stdin=file("/dev/null"),
  96.                                          stdout=subprocess.PIPE,
  97.                                          stderr=subprocess.PIPE)
  98.                     result = p.run ()
  99.                     self.answers['nmblookup_output'] = result
  100.                     for line in result[0]:
  101.                         if line.startswith ("querying"):
  102.                             continue
  103.                         spc = line.find (' ')
  104.                         if (spc != -1 and
  105.                             not line[spc:].startswith (" failed ")):
  106.                             # Remember the IP address.
  107.                             self.answers['remote_server_name'] = line[:spc]
  108.                             break
  109.                 except OSError:
  110.                     # Problem executing command.
  111.                     pass
  112.             elif scheme == "hp":
  113.                 os.environ['URI'] = uri
  114.                 try:
  115.                     p = TimedSubprocess (parent=parent,
  116.                                          timeout=3000,
  117.                                          args='LC_ALL=C DISPLAY= hp-info -d"$URI"',
  118.                                          close_fds=True,
  119.                                          shell=True,
  120.                                          stdin=file("/dev/null"),
  121.                                          stdout=subprocess.PIPE,
  122.                                          stderr=subprocess.PIPE)
  123.                     self.answers['hplip_output'] = p.run ()
  124.                 except OSError:
  125.                     # Problem executing command.
  126.                     pass
  127.  
  128.             r = cups_printer_dict['printer-type'] & cups.CUPS_PRINTER_REMOTE
  129.             self.answers['cups_printer_remote'] = (r != 0)
  130.         return False
  131.  
  132.     def collect_answer (self):
  133.         return self.answers
  134.